﻿using System;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Collections.Generic;

namespace WAD.DAL
{
    /// <summary>
    /// 广告分享名额操作
    /// </summary>
    public class DAOFCSharedMoney : DAO
    {
        /// <summary>
        /// 批量生成红包
        /// </summary>
        public static int Add_HongBao(int article_id, int number)
        {
            Database database = GetDatabase();
            if (number < 100)
            {
                //不足100个红包数，红包全额发放
                DbCommand cmd = database.GetStoredProcCommand("FCShared_Money_HongBao_100");
                database.AddInParameter(cmd, "@article_id", DbType.Int32, article_id);
                return database.ExecuteNonQuery(cmd);
            }
            else
            {
                //大于100个红包,扣除返利费用，在发放
                DbCommand cmd = database.GetStoredProcCommand("FCShared_Money_HongBao");
                database.AddInParameter(cmd, "@article_id", DbType.Int32, article_id);
                return database.ExecuteNonQuery(cmd);
            }
        }
        /// <summary>
        /// 批量生成红包，微信专用
        /// </summary>
        public static int Add_HongBao2(int article_id, int number)
        {
            Database database = GetDatabase();
            if (number < 100)
            {
                //不足100个红包数，红包全额发放
                DbCommand cmd = database.GetStoredProcCommand("FCShared_Money_HongBao_100");
                database.AddInParameter(cmd, "@article_id", DbType.Int32, article_id);
                return database.ExecuteNonQuery(cmd);
            }
            else
            {
                //大于100个红包,扣除返利费用，在发放
                DbCommand cmd = database.GetStoredProcCommand("FCShared_Money_HongBao_2");
                database.AddInParameter(cmd, "@article_id", DbType.Int32, article_id);
                return database.ExecuteNonQuery(cmd);
            }
        }

        /// <summary>
        /// 抢红包名额，抢到了返回ID
        /// </summary>
        /// <param name="user_id"></param>
        /// <param name="article_id"></param>
        /// <returns></returns>
        public static int Qiang(int user_id, int article_id)
        {
            Database database = GetDatabase();
            DbCommand cmd = database.GetStoredProcCommand("FCSharedMoney_Qiang");
            database.AddInParameter(cmd, "@user_id", DbType.Int32, user_id);
            database.AddInParameter(cmd, "@article_id", DbType.Int32, article_id);
            database.AddOutParameter(cmd, "@id", DbType.Int32, 4);
            database.ExecuteNonQuery(cmd);
            object o = database.GetParameterValue(cmd, "id");
            if (o != null)
                return Convert.ToInt32(o);
            return 0;
        }

        /// <summary>
        /// 获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Dictionary<string, object> Get(int id)
        {
            return Get("fc_shared_money", "id=" + id);
        }

        /// <summary>
        /// 设置分享状态
        /// </summary>
        /// <returns></returns>
        public static int UpdateState(int id, int state)
        {
            return ExecuteNonQuery(string.Format("update fc_shared_money set [state]={0} where id={1}", state, id));
        }

        /// <summary>
        /// 当分享后，锁定红包ID，避免红包超过5分钟释放
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int Lock(int id)
        {
            return ExecuteNonQuery(string.Format("update fc_shared_money set [lock]=1 where id={0}", id));
        }
    }
}